<?php

namespace App\Http\Controllers\Api;

use App\Bid;
use App\Bid_Goods;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class BidGoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->item) {
            return Bid_Goods::with(['cate', 'bid.user:id,username', 'craftsman:id,rank,shop_name,user_id'])->find($request->item);
        }

        if ($request->craftsman) {
            return Bid_Goods::where('craftsman_id', $request->craftsman)->with(['cate', 'bid.user:id,nickname,avatar'])->Paginate(6);
        }

        $goods = Bid_Goods::with(['bid.user:id,nickname'])->where([
            ['title', 'like', '%' . $request->key . '%']
        ]);

        switch ($request->status) {
            case 0:
                $goods = $goods->where('status', 0);
                break;
            case 1:
                $goods = $goods->where('status', 1);
                break;
            case 2:
                $goods = $goods->where('status', 2);
                break;
            case 3:
                $goods = $goods->where('status', 3);
                break;

            default:
                break;
        }
        switch ($request->sort) {
            case 1:
                $goods = $goods->orderBy('max_price', 'asc');
                break;
            case 2:
                $goods = $goods->orderBy('max_price', 'desc');
                break;
            case 3:
                $goods = $goods->orderBy('bid_count', 'asc');
                break;
            case 4:
                $goods = $goods->orderBy('bid_count', 'desc');
                break;

            default:
                break;
        }

        if ($request->cate_id) {
            return $goods->where('cate_id', $request->cate_id)->Paginate(8);
        }

        return $goods->Paginate(8);
    }

    public function updateOrCreate(Request $request)
    {
        if ($request->id) { // 更新
            $bidGoods = Bid_Goods::find($request->id);
            return $bidGoods->update($request->all());
        }

        // 获取文件信息
        $file = $request->file('image_src');
        $clientName = $_FILES['image_src']['name'];
        $extension = explode('.', $clientName);
        // 文件名加密
        $newName = md5(date('ymdhis') . $clientName) . "." . $extension[1];
        $file->move("storage/bid_goods/{$request->craftsman_id}", $newName);

        return Bid_Goods::create([
            'craftsman_id' => $request->craftsman_id,
            'title' => $request->title,
            'cate_id' => $request->cate_id,
            'desc' => $request->desc,
            'image_src' => Str::start($newName, "http://craftsman.test/storage/bid_goods/{$request->craftsman_id}/"),
            'init_price' => $request->init_price,
            'min_gap' => $request->min_gap,
            'max_price' => $request->max_price,
            'start_time' => $request->start_time,
            'end_time' => $request->end_time,
        ]);
    }

    public function bid(Request $request)
    {
        Bid_Goods::find($request->bid_goods_id)
            ->update(['max_price' => $request->price]);
        return Bid::create([
            'bid_goods_id' => $request->bid_goods_id,
            'user_id' => $request->user_id,
            'price' => $request->price,
            'bid_time' => now()->addHour(8),
        ])->with(['user:id,username'])->get()->pop();
    }

    public function destroy(Bid_Goods $bidGoods)
    {
        $oldFile = Str::after($bidGoods->image_src, "http://craftsman.test/storage/");
        Storage::delete(Str::finish("public/", $oldFile));
        $bidGoods->delete();
        return 'OK';
    }
}
